package com.sandking.db.cfg;

import java.util.ArrayList;
import com.sandking.tools.SK_Map;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.sandking.config.SK_Config;
import com.sandking.tools.SK_Collections;
import java.util.Map;
import com.sandking.tools.SK_Plus;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sandking.tools.SK_List;
import java.sql.Connection;
import java.lang.Exception;
import com.sandking.io.SK_InputStream;
import org.apache.commons.dbutils.DbUtils;
import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * 邮件类型
 */
public class YjlxCfg {

	public static final String TABLENAME = "邮件类型";
	private static boolean isLoadAll = false;
	
	public static boolean isLoadAll() {
		return isLoadAll;
	}

	public static void setLoadAll(boolean isLoadAll) {
		YjlxCfg.isLoadAll = isLoadAll;
	}
	
	//缓存
	static final Map<String, YjlxCfg> idCache = SK_Collections.newSortedMap();

	/** id */
	private int id;
	
	/** 名称 */
	private String mc;
	
	
	public YjlxCfg() {
		super();
	}
	
	public YjlxCfg(int id, String mc) {
		super();
		this.id = id;
		this.mc = mc;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	public String getMc() {
		return mc;
	}
	
	public void setMc(String mc) {
		this.mc = mc;
	}
	
	 /**
     * 根据list创建对象
     */
    public static List<YjlxCfg> createForColumnNameList(List<Map<String, Object>> list){
    	List<YjlxCfg> yjlxCfgs = new ArrayList<YjlxCfg>();
		for (Map<String, Object> map : list) {
			yjlxCfgs.add(createForColumnNameMap(map));
		}
		return yjlxCfgs;
    }
    
    /**
     * 根据map创建对象
     */
    public static YjlxCfg createForColumnNameMap(Map<String, Object> map){
    	YjlxCfg obj = new YjlxCfg();
	    obj.id = SK_Map.getInt("id", map);
	    obj.mc = SK_Map.getString("名称", map);
        return obj;
    }
    
    public void toStream(ByteArrayOutputStream out) throws Exception {
	    SK_OutputStream.writeInt(out,this.id);
	    SK_OutputStream.writeString(out,this.mc);
    }
    
     public static YjlxCfg forStream(ByteArrayInputStream in) throws Exception {
     	YjlxCfg yjlxCfg = new YjlxCfg();
	    yjlxCfg.id = SK_InputStream.readInt(in,null);
	    yjlxCfg.mc = SK_InputStream.readString(in,null);
	    return yjlxCfg;
     }
    
    public static List<YjlxCfg> loadDatabase(){
		Connection conn = SK_Config.getConnection();
		return loadDatabase(conn);
	}
	
	public static List<YjlxCfg> loadDatabase(Connection conn){
		return loadDatabase(conn,YjlxCfg.TABLENAME);
	}
	
	public static List<YjlxCfg> loadDatabase(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return loadDatabase(conn);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static List<YjlxCfg> loadDatabase(String tableName){
		Connection conn = SK_Config.getConnection();
		return loadDatabase(conn,tableName);
	}
	
	public static List<YjlxCfg> loadDatabase(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,名称 FROM " + tableName;
		List<YjlxCfg> yjlxCfgs = null; 
		try {
			List<Map<String,Object>> list = run.query(conn, sql, new MapListHandler());
			yjlxCfgs = YjlxCfg.createForColumnNameList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				e1.printStackTrace();
				return null;
			}
		}
		return yjlxCfgs;
	}
	
	public static List<YjlxCfg> loadDatabase(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return loadDatabase(conn,tableName);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static void writeFile(String path) throws Exception{
		try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
			List<YjlxCfg> yjlxCfgs = loadDatabase();
			SK_OutputStream.writeInt(out, yjlxCfgs.size());
			for(YjlxCfg yjlxCfg : yjlxCfgs){
				yjlxCfg.toStream(out);
			}
			FileUtils.writeByteArrayToFile(new File(path), out.toByteArray());
		}catch (Exception e) {
            throw e;
        }
	}
	
	public static void loadFile(String path) throws Exception{
		byte[] data = FileUtils.readFileToByteArray(new File(path));
		try (ByteArrayInputStream in = new ByteArrayInputStream(data);) {
			int size = SK_InputStream.readInt(in, null);
			for (int i = 0; i < size; i++) {
				loadCache(forStream(in));
			}
			isLoadAll = true;
		}catch (Exception e) {
            throw e;
        }
	}
	
	/**
	 * 加载缓存
	 */
	public static void loadCache(YjlxCfg yjlxCfg){
		idCache.put(SK_Plus.b(yjlxCfg.getId()).e(),yjlxCfg);
	}
	
	/**
	 * 根据( id ) 查询
	 */
	public static YjlxCfg getById(int id){
		YjlxCfg yjlxCfg = null;
		String key = SK_Plus.b(id).e();
		yjlxCfg = idCache.get(key);
		
		return yjlxCfg;
	}
	
	
	public static List<YjlxCfg> getAll(){
		return new ArrayList<YjlxCfg>(idCache.values());
	}
	
	public static List<YjlxCfg> getAll(int page,int size,Integer pageCount){
		List<YjlxCfg> yjlxCfgs = getAll();
		yjlxCfgs = SK_List.getPage(yjlxCfgs, page, size, pageCount);
		return yjlxCfgs;
	}
}